/*
    alert("1:"+document.location);
    alert("2"+document.location.href);
    alert("3"+document.location.hash);
    alert("4"+document.location.host);

    alert("5:"+document.location.hostname);
    alert("6"+document.location.pathname);
    alert("7"+document.location.port);
    alert("8"+document.location.protocol);
*/

$(document).ready(function (){
    var interval;
    var url = "";
    /**
     * 进入admin模块的时候弹出登录框
     * url为目标地址
     * @author : nonglei
     * @date   : 20/03/2014
     */
   /* $("#adminManage").click(function(){
        if(!($("#loginFlag").attr("value") == '0'))
        {
            url = $("#loginModalText #url").attr("value") + "/admin";
            $('#loginModal').modal();
        }
        else
        {
            location.href = $("#loginModalText #url").attr("value") + '/admin';
        }
    });
	$("#loginModalText #login-submit").click(function(){
		var username = $("#loginModalText .loginModalUsername").val();
		var password = $("#loginModalText .loginModalPassword").val();
		var remenberme = $("#loginModalText .loginModalCheckbox").is(':checked');
		loginAjax(username,password,remenberme,url);
	});
    $("#translationEN").click(function(){
        translation('en');
    });
    $("#translationCN").click(function(){
        translation('zh_cn');
    });*/

    $('#loginboxform').on('click', '[type="submit"]', function() {
        $.ajax({
            'type': 'post',
            'url': $("#loginboxform").attr('url'),
            'data': $("#login-form").serialize(),
            success: function(data) {
                if (data === 'success')
                {
                    history.go(0);
                }
                $('#loginboxform').html(data);
            }
        });

        return false;
    });

});

function loginAjax(username,password,remenberme,url)
{
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp = new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function()
	{
	  if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	  {
		 if(xmlhttp.responseText == 0)//pass
		 {
            if(url == "")
            {
			    location.replace(location.href);
            }
            else
            {
			    location.replace(url);
            }
		 }
		 else if(xmlhttp.responseText == 1)//username wrong
		 {
			 $("#loginModalText .usernameText").addClass("error");
			 $("#loginModalText .loginModalUsername").addClass("errorBox");
			 $("#loginModalText .passwordText").removeClass("error");
			 $("#loginModalText .loginModalPassword").removeClass("errorBox");
		 }
		 else//password wrong
		 {
			 $("#loginModalText .passwordText").addClass("error");
			 $("#loginModalText .loginModalPassword").addClass("errorBox");
			 $("#loginModalText .usernameText").removeClass("error");
			 $("#loginModalText .loginModalUsername").removeClass("errorBox");
         }
	  }
	}
    var urlStr = $("#loginModalText #url").attr("value") + "/site/login";
    xmlhttp.open("POST",urlStr,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
	xmlhttp.send("username=" + username + "&password=" + password + "&remenberme=" + remenberme);
}
//js获取项目根路径，如： http://localhost:8083/uimcardprj
function getRootPath()
{   
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    //var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    //var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    //var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj/
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(projectName);
}
/**
 * preview headImage picture
 * @author : nonglei
 * @date   : 01/05/2014
 */
function headImagePreview()
{
    var file = $("#headImageView")[0].files[0];
    var img = $("#imageViewer")[0];
    if(file && img)
    {
        if(file.size >= 1024*1024)
        {
            alert("请上传小于1M的图片");
            /** cancel the selecting of file **/
            document.getElementById("headImageView").outerHTML = document.getElementById("headImageView").outerHTML;
            return;
        }
        var url = window.URL.createObjectURL(file);
        img.src = url;
    }
}
/**
 * preview newsImage picture
 * @author : nonglei
 * @date   : 017/05/2014
 */
function newsImagePreview()
{
    var file = $("#newsImageView")[0].files[0];
    var img = $("#imageViewer")[0];
    if(file && img)
    {
        if(file.size >= 5*1024*1024)
        {
            alert("请上传小于5M的图片");
            /** cancel the selecting of file **/
            document.getElementById("newsImageView").outerHTML = document.getElementById("newsImageView").outerHTML;
            $("News_url").val("");
            return;
        }
        var url = window.URL.createObjectURL(file);
        img.src = url;
    }
}

/**
 * check the file type and size
 * @author : nongleo
 * @date   : 06/05/2014
 */
function checkFile(file,limitSize,limitType)
{

}
/**
 * AJAX Upload File
 * @author : nonglei
 * @date   : 01/05/2014
 */
function ajaxFileUpload(urlStr,fileID,callBack)
{
    $.ajaxFileUpload({
        url:urlStr,
        secureuri:false,
        fileElementId:fileID,
        dataType: 'txt',
        success: callBack,
    });
        return false;
}
/**
 * ajax sent
 * @author : nonglei
 * @date   : 06/05/2014
 */
function ajaxConstruction(urlArg, dataArg, dataTypeArg, successArg)
{
    $.ajax({
        url:urlArg,
        type:"POST",
        data:dataArg,
        dataType:dataTypeArg,
        success:successArg,
    });
}
/**
 * Change Tranlation language
 * @author : nonglei
 * @date   : 12/05/2014
 */
function translation(languageStr)
{
    var url = $("#loginModalText #url").attr("value")+'/site/translationselect';
    var dataArg = 'language='+languageStr;
    var success = function(data){
        location.replace(location.href);
    };
    ajaxConstruction(url,dataArg,'text',success);
}
